﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;

public partial class backstage_group_mail : System.Web.UI.Page
{
    // Fields
    protected UserManager um = new UserManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            SendMailMessage message = new SendMailMessage();
            Array array = base.Request.Form["addUser"].Split(new char[] { ',' });
            message.subject = this.txtSobject.Value;
            message.body = this.fckBody.Value;
            message.html = true;
            DataTable allEnabledMail = new MailManager().GetAllEnabledMail();
            Random random = new Random();
            for (int i = 0; i < array.Length; i++)
            {
                message.mailTo = array.GetValue(i).ToString();
                int num2 = random.Next(allEnabledMail.Rows.Count - 1);
                DataRow row = allEnabledMail.Rows[num2];
                message.from = row["fromAddress"].ToString();
                message.fromName = row["fromName"].ToString();
                message.mailDomain = row["mailDomain"].ToString();
                message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                message.mailServerUserName = row["mailServerUserName"].ToString();
                message.mailServerPassWord = row["mailServerPassWord"].ToString();
                message.SendMailEvent += new SendMailMessage.SendMailHandler(this.SendMain);
                new Thread(new ThreadStart(message.Send)).Start();
            }
            Thread.Sleep(100);
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送成功！\");location.href=\"mail_list.aspx\";</script>");
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送失败！\");</script>");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
    }

    protected void SendMain()
    {
    }

    protected StringBuilder UserBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable allUserEmailByLanguageVersion = this.um.GetAllUserEmailByLanguageVersion(this.Session["language_version"].ToString());
            foreach (DataRow row in allUserEmailByLanguageVersion.Rows)
            {
                builder.AppendLine("<option value=\"" + row["email"].ToString() + "\">" + row["userName"].ToString() + "</option>");
            }
        }
        catch
        {
        }
        return builder;
    }

}